package imageProcessing;

import java.util.ArrayList;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

public class Loader implements ImageListener {

	private String _name;
	private PlanarImage _image;
	
	private ArrayList<ImageListener> _listeners;
	
	ImageEvent _iE;
	
	public Loader(){
		_name = "";
		_image = null;
		_listeners = new ArrayList<ImageListener>();
	}
	
	public void notifyListeners() {		
		ArrayList<ImageListener> copy = Duplicator.copyListeners(_listeners);
		
		for (ImageListener listener : copy) {
			listener.imageChanged(_iE);
		}
	}
	
	public String getName(){
		return this._name;
	}
	
	public void addImageListener(ImageListener il){
		_listeners.add(il);
	}
	public void removeImageListener(ImageListener li){
		_listeners.remove(li);
	}
	
	public void setName(String name){
		_name = name;
		try {
			_image = JAI.create("fileload", _name);
			_iE  = new ImageEvent(this, _image);
			notifyListeners();
			
		} catch (IllegalArgumentException e) {
			System.out.println("File not found. No Name set?");
		}
	}

	@Override
	public void imageChanged(ImageEvent imageEvent) {
		if(!_name.equals(""))
		{
			setName(_name);
		}
		else
		{
			System.out.println("No name set yet");
		}
		
	}
}
